//主页面框架
#include "XMainWindow.h"
#include "ui_mainwindow.h"
#include <QFont>
#include <QDebug>
#include <string>
#include "logs.h"

XLabel::XLabel(QWidget* parent): QLabel(parent)
{
}

void XLabel::enterEvent(QEvent *event)
{
    emit xlabel_in();
 //   if(user_info->isHidden())
   // {
       // user_info->move(ui->Account->x()-20,ui->Account->y()+ui->Account->height());
        //user_info->show();
    //}
}

void XLabel::leaveEvent(QEvent *event)
{
    emit xlabel_out();
}


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
//ui窗体初始化
    ui->setupUi(this);

    Account = new XLabel(this);
    Account->setMinimumSize(50,50);
    Account->setText("wwww");
    user_info = nullptr;
    ui->horizontalLayout_2->addWidget(Account);

    this->setWindowState(Qt::WindowMaximized);
    all_font.setPixelSize(20);
    all_font.setFamily("HONOR Sans Text GBK");  //使用字体
    all_font.setPointSize(15);
    setWindowIcon(QIcon(":/img/204308.png"));//窗口图标
    setWindowTitle("后台管理");                //窗口标题

    //初始化菜单按钮文本和子菜单个数
    leftbar.push_back(std::make_pair<QString,int>("首页",0));
    leftbar.push_back(std::make_pair<QString,int>("外观",1));
    leftbar.push_back(std::make_pair<QString,int>("论坛",1));


    //一级按钮
    btn.reset(new  QPushButton[leftbar.size()]);

    //二级窗口,设置最大数目<5
    twowin.reset(new QWidget[5]);

    //标题栏


    int i=0;
   //对一级菜单按钮初始化和设置事件
    for(auto pair : leftbar)
    {
        btn[i].setFont(all_font);
        btn[i].setParent(ui->sidebar);
        btn[i].setText(pair.first);
        btn[i].setFixedHeight(50);
        btn[i].setStyleSheet("color:rgb(186,186,186);background:transparent;");
        ui->sidebarLayout->addWidget(&btn[i]); //按钮添加到布局中去

        //创建n个widget添加子集按钮
        Windowadd(i,pair.second);

        connect(&btn[i],&QPushButton::clicked,this,[=]()
        {//对外界的值进行复制操作
           if(i==ischick)return;
        btn[ischick].setStyleSheet("color:rgb(186,186,186);background:transparent;");//取消上一个选中状态
       //收纳上次选择的按钮
        if(!(twowin[ischick].isHidden()))
        {
            twowin[ischick].hide();
        }

        //创建按钮对应的窗口
        if(pair.first=="首页")
        {
            std::unique_ptr<M_Win> m_index(new M_Index);//创建对应接口类
            window=m_index->factoryMethod();            //使用接口生成类
            window.get()->setParent(ui->win);
        }else if(pair.first=="外观")
        {
            std::unique_ptr<M_Win> m_apent(new M_Apent);//创建对应接口类
            window=m_apent->factoryMethod();            //使用接口生成类
            window.get()->setParent(ui->win);
        }else if(pair.first == "论坛")
        {

        }
        ui->winLayout->addWidget(window.get());
        ischick=i;
        btn[i].setStyleSheet("background-color:rgb(30,144,255);");
        if(pair.second<=0)
        {return ;}
        if(twowin[i].isHidden())
        {
            twowin[i].show();
        }else{
            twowin[i].hide();
        }
        });

        i++;
    }

    //默认选中首页
     btn[0].setStyleSheet("background-color:rgb(30,144,255);");
     //初始化选中状态
     ischick=0;
     //设置一级菜单的垂直弹簧
      verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
      ui->sidebarLayout->addItem(verticalSpacer);

      //默认首页    工厂模式
     std::unique_ptr<M_Win> m_index(new M_Index);//创建对应接口类
     window=m_index->factoryMethod();            //使用接口生成类
     window->setParent(ui->win);//将indx界面加入palydata布局中
     ui->winLayout->addWidget(window.get());


     user_info = new Account_INFO(this);
     user_info->hide();
     connect(user_info,&Account_INFO::closeAccount,this,[=](){
      on_swap_clicked();
      });

    QString PotPath = JsonConfigManager::instance()->get("path","").toString();
    QPixmap pix(PotPath);
    pix = pix.scaled(
                Account->width(),
                Account->height(),
                Qt::KeepAspectRatio,  // 保持宽高比
                Qt::SmoothTransformation  // 平滑缩放
            );
   Account->setPixmap(pix);


   connect(Account,&XLabel::xlabel_in,this,[=](){
     user_info->move(Account->x()-Account->width()-50,Account->y()+Account->height());
       user_info->show();
   });

   connect(Account,&XLabel::xlabel_out,this,[=](){
        if(user_info->getEnter())
        {
            return;
        }
       user_info->hide();
   });

   connect(this,&MainWindow::clicked,this,[&](){
       if(!user_info->getEnter())
       {
           user_info->hide();
       }
   });


}

MainWindow::~MainWindow()
{
    delete verticalSpacer;
    delete ui;
}


//子集按钮
void MainWindow::Windowadd(int i,int btnNumber){
    if(btnNumber<=0)
    {
        return ;
    }
    int sizei=i;
    //对二级菜单栏初始
    //Widget设置空间
    twowin[sizei].setFixedHeight(btnNumber*50);

   // ui->meun_verticalLayout->addWidget(&twowin[i]);
    QVBoxLayout *Secondvert=new QVBoxLayout(&twowin[sizei]);
    Secondvert->setSpacing(0);
    Secondvert->setContentsMargins(0, 0, 0, 0);
     for(int j=0;j<btnNumber;j++)
     {
        QPushButton  *SecondBtn=new QPushButton(&twowin[sizei]);
        SecondBtn[j].setFixedHeight(50);
        SecondBtn[j].setText("on");
        Secondvert->addWidget(SecondBtn);
     }

    //设置二级窗口颜色
    twowin[sizei].setStyleSheet("background-color: rgb(66, 66, 66);");
   //把布局添加到窗口中去
   twowin[sizei].setLayout(Secondvert);
//隐藏窗口
   twowin[sizei].hide();
}


void MainWindow::on_exitbnt_clicked()
{
    this->close();
}

void MainWindow::on_swap_clicked()
{

    this->hide();
    emit reaccount();

}


void MainWindow::on_radioButton_clicked()
{
    if( ui->sidebar->isHidden())
    {
        ui->sidebar->show();
    }else
    {
        ui->sidebar->hide();
    }

}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    emit clicked();

}




//用户信息QLabel
//void MainWindow::on_Account_linkHovered(const QString &link)
//{


    //}else
  //  {
        //   delete user_info;
//           user_info=nullptr;
    //    user_info->hide();





     //   }
//}
